# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=Pillow
pkgbase=mingw-w64-python-pillow
pkgname=("${MINGW_PACKAGE_PREFIX}-python-pillow")
pkgver=8.2.0
pkgrel=1
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}=${pkgver}"
          "${MINGW_PACKAGE_PREFIX}-python3-pillow=${pkgver}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}"
           "${MINGW_PACKAGE_PREFIX}-python3-pillow")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}"
          "${MINGW_PACKAGE_PREFIX}-python3-pillow")
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
license=('custom')
url="https://github.com/python-pillow/Pillow/"
pkgdesc="Python Imaging Library (PIL) fork Python3 version (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libimagequant"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-libraqm"
             "${MINGW_PACKAGE_PREFIX}-tk")
optdepends=("${MINGW_PACKAGE_PREFIX}-tk: for the ImageTK module"
            "${MINGW_PACKAGE_PREFIX}-libraqm: complex text layout support")
options=('staticlibs')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/python-pillow/Pillow/archive/${pkgver}.tar.gz)
sha256sums=('da86280bbfcd2b7fd87d33ce6f28688bf600600dfdbe85bfc16c562bbd26ca97')

prepare() {
  cd ${srcdir}
  rm -rf python-build-${CARCH} | true
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"
}

build() {
  cd "${srcdir}"
  msg "Python build for ${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
      ${MINGW_PREFIX}/bin/python setup.py build
}

check() {
  msg "Python test for ${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
      ${MINGW_PREFIX}/bin/python selftest.py || true

  #setup.py test
}

package() {
  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})
  local _pybasever=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")
  
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py install \
   --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}/" \
   --optimize=0 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
  install -dm755 "${pkgdir}${MINGW_PREFIX}/include/python${_pybasever}/"
  install -m644 -t "${pkgdir}${MINGW_PREFIX}/include/python${_pybasever}/" src/libImaging/*.h

  # clean up bins
  #  cd "${pkgdir}${MINGW_PREFIX}/bin"
  #  for f in *.py; do
  #    sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i ${f}
  #    mv "$f" "${f%.py}"
  #  done
}
